package com.it135.citytraffic.controller;

import com.it135.citytraffic.pojo.dto.TrafficInfoAverageDTO;
import com.it135.citytraffic.pojo.dto.TrafficInfoPageQueryDTO;
import com.it135.citytraffic.pojo.entity.TrafficInfo;
import com.it135.citytraffic.pojo.result.PageResult;
import com.it135.citytraffic.pojo.result.Result;
import com.it135.citytraffic.pojo.vo.TrafficInfoAverageVO;
import com.it135.citytraffic.pojo.vo.TrafficInfoVO;
import com.it135.citytraffic.service.ITrafficInfoService;
import com.it135.citytraffic.utils.BeanCopyTool;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;


import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 交通情况
 */
@Slf4j
@RestController
@Data
@RequestMapping("/api/trafficInfo")
public class TrafficController {
    private final ITrafficInfoService trafficInfoService;
    /**
     * @return
     */
    @CrossOrigin
     @GetMapping()
    public Result<List<TrafficInfoVO>> saveTrafficInfo() {
        List<TrafficInfo> trafficInfos = trafficInfoService.list();
        List<TrafficInfoVO> trafficInfoVOS = BeanCopyTool.convertCopyList(trafficInfos, TrafficInfoVO.class);
        return Result.success(trafficInfoVOS);
     }
    /**
     * 交通情况分页查询
     *
     * @param trafficInfoPageQueryDTO
     * @return
     */
    @CrossOrigin
    @GetMapping("/trafficInfoPage")
    public Result<PageResult> pageQueryTrafficInfo(TrafficInfoPageQueryDTO trafficInfoPageQueryDTO) {
        PageResult pageResult = trafficInfoService.pageQuery(trafficInfoPageQueryDTO);

        return Result.success(pageResult);
     }

    /**
     * 根据条件,获取交通拥堵情况的平均值
     * @param trafficInfoAverageDTO
     * @return
     */
    @CrossOrigin
    @GetMapping("/average")
    public Result<TrafficInfoAverageVO> getAverage(TrafficInfoAverageDTO trafficInfoAverageDTO) {
        return Result.success(trafficInfoService.getInfoAvg(trafficInfoAverageDTO));
     }
    @CrossOrigin
    @DeleteMapping
    public Result<String> DeleteCrossingList(@RequestParam List<Long> ids) {
        trafficInfoService.removeByIds(ids);
        return Result.success("删除成功");
     }

}
